﻿using System;
using MySql.Data.MySqlClient;
using System.Data;

namespace MySQLLib
{
    public class Connection
    {
        private string ConnectionString;
        private MySqlConnection mysqlconnection;

        public Connection(string ip, string database, string userid, string password, bool connect = false)
        {
            ConnectionString = "SERVER=" + ip + ";" +
                            "DATABASE=" + database + ";" +
                            "UID=" + userid + ";" +
                            "PASSWORD=" + password + ";";

            if (connect)
            {
                Connect();
            }

        }

        public bool Connect()
        {
            try
            {
                mysqlconnection = new MySqlConnection(ConnectionString);
                mysqlconnection.Open();
            }
            catch (MySqlException)
            {
                return false;
            }

            return true;
        }

        public MySqlConnection GetConnection()
        {
            return mysqlconnection;
        }

        public ConnectionState GetConnectionState()
        {
            return mysqlconnection.State;
        }

        public void Close()
        {
            mysqlconnection.Close();
            mysqlconnection.Dispose();
        }
    }
}
